﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UdrinkWater_Project
{
    public partial class RegisterCustomer : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public static object GetIsNull(object value)
        {
            if (value == null || value == "")
            {
                return DBNull.Value;
            }

            return value;
        }
        private void InsertCustomer()
        {
            string sqltext = @"  insert into TB_Customer
		                 ([ID_Card]
                        ,[Prefix]
                        ,[Firstname]
                        ,[Lastname]
                        ,[Adress]
                        ,[DISTRICT_ID]
                        ,[AMPHUR_ID]
                        ,[PROVINCE_ID]
                        ,[Phone]
                        ,[E_mail]
                        ,[Fax]
                        ,[Zipcode]
                        ,[Pasword]
                        ,[Status])
                        values (@id,@pf,@fn,@ln,@add,@dis,@amp,@pro,@tel,@Mail,@Fax,@Zip,@Pass,@Status)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlcon = new SqlConnection(Connection);
            sqlcomm.Connection = sqlcon;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            sqlcomm.Parameters.AddWithValue("@id", GetIsNull(txtID.Text));
            sqlcomm.Parameters.AddWithValue("@pf", GetIsNull(cb_Prefix.SelectedItem.Text));
            sqlcomm.Parameters.AddWithValue("@fn", GetIsNull(txtFname.Text));
            sqlcomm.Parameters.AddWithValue("@ln", GetIsNull(txtLname.Text));
           //sqlcomm.Parameters.AddWithValue("@dp", GetIsNull(cb_Department.Value));
            sqlcomm.Parameters.AddWithValue("@add", GetIsNull(txtAdress.Text));
            sqlcomm.Parameters.AddWithValue("@dis", GetIsNull(cb_District.Value));
            sqlcomm.Parameters.AddWithValue("@amp", GetIsNull(cb_Aumphur.Value));
            sqlcomm.Parameters.AddWithValue("@pro", GetIsNull(cb_Province.Value));
            sqlcomm.Parameters.AddWithValue("@tel", GetIsNull(txtPhone.Text));
            sqlcomm.Parameters.AddWithValue("@Mail", GetIsNull(txtmail.Text));
            sqlcomm.Parameters.AddWithValue("@Fax", GetIsNull(txtFax.Text));
            sqlcomm.Parameters.AddWithValue("@Zip", GetIsNull(txt_Zipcode.Text));
            encryption_MD5 MD5 = new encryption_MD5();
            string PassWord = MD5.EncryptData(txtPass.Text);
            sqlcomm.Parameters.AddWithValue("@Pass", GetIsNull(PassWord));
            sqlcomm.Parameters.AddWithValue("@Status", GetIsNull(1));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();
        }
        private bool CheckID(string Usertext)
        {
            bool chk = false;
            if (Usertext == "")
            {
                return true;
            }
            else
            {
                string sqltext = @"SELECT ID_Card FROM TB_Customer WHERE ID_Card = '" + Usertext + "'";
                SqlConnection conn = new SqlConnection(Connection);
                SqlDataAdapter dap = new SqlDataAdapter(sqltext, conn);
                DataTable dt = new DataTable();
                DataSet ds = new DataSet();
                dap.Fill(ds);
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    chk = true;
                }
                else
                {
                    chk = false;
                }
                return chk;
            }
        }

        protected void bt_Register_Click(object sender, EventArgs e)
        {
            bool chk = CheckID(txtID.Text);
            if (chk == true)
            {
                Response.Write(@"<script language='javascript'>alert('เลขที่บัตรนี้เคยใช้แล้ว')</script>");
            }
            else
            {
                InsertCustomer();
            }
           
        }
    }
}